package hos.filex.delegate;

import android.net.Uri;
import android.os.Build;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

import java.util.List;

import hos.file.media.FileType;


/**
 * <p>Title: ImageDelegate </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-14 15:03
 */
public class FileDelegate extends CameraDelegate {

    @NonNull
    public static FileDelegate create(ComponentActivity activity) {
        return new FileDelegate(activity);
    }

    @NonNull
    public static FileDelegate create(Fragment fragment) {
        return new FileDelegate(fragment);
    }

    protected FileDelegate(@NonNull LifecycleOwner lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        getContent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            getContentList();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getDocument();
            getDocumentList();
        }
    }

    @Nullable
    private ActivityResultLauncher<FileType.Type> contentLauncher;

    /**
     * 选择照片
     */
    @NonNull
    private ActivityResultLauncher<FileType.Type> getContent() {
        if (contentLauncher != null) {
            return contentLauncher;
        }
        return contentLauncher = FileResultContracts.getContent(requireActivityResultCaller(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                handleUri(uri);
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<FileType.Type> contentListLauncher;

    /**
     * 选择多张照片
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @NonNull
    private ActivityResultLauncher<FileType.Type> getContentList() {
        if (contentListLauncher != null) {
            return contentListLauncher;
        }
        return contentListLauncher = FileResultContracts.getContentList(requireActivityResultCaller(), new ActivityResultCallback<List<Uri>>() {
            @Override
            public void onActivityResult(List<Uri> uriList) {
                handleUriList(uriList);
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<FileType.Type[]> documentLauncher;

    /**
     * 选择多张照片
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @NonNull
    private ActivityResultLauncher<FileType.Type[]> getDocument() {
        if (documentLauncher != null) {
            return documentLauncher;
        }
        return documentLauncher = FileResultContracts.getDocument(requireActivityResultCaller(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                handleUri(uri);
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<FileType.Type[]> documentListLauncher;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @NonNull
    private ActivityResultLauncher<FileType.Type[]> getDocumentList() {
        if (documentListLauncher != null) {
            return documentListLauncher;
        }
        return documentListLauncher = FileResultContracts.getDocumentList(requireActivityResultCaller(), new ActivityResultCallback<List<Uri>>() {
            @Override
            public void onActivityResult(List<Uri> uriList) {
                handleUriList(uriList);
            }
        });
    }

    //<editor-fold desc="ALL">
    public void chooseContentAll(FileResult.OnResultListener listener) {
        chooseContent(listener, FileType.Type.ALL);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void chooseContentListAll(FileResult.OnResultListListener listener) {
        chooseContentList(listener, FileType.Type.ALL);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseDocumentAll(FileResult.OnResultListener listener) {
        chooseDocument(listener, FileType.Type.ALL);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseDocumentListAll(FileResult.OnResultListListener listener) {
        chooseDocumentList(listener, FileType.Type.ALL);
    }
    //</editor-fold>

    //<editor-fold desc="Content">
    public void chooseContent(FileResult.OnResultListener listener, FileType.Type fileType) {
        mOnResultListener = listener;
        try {
            getContent().launch(fileType);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void chooseContentList(FileResult.OnResultListListener listener, FileType.Type fileType) {
        mOnResultListListener = listener;
        try {
            getContentList().launch(fileType);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }
    //</editor-fold>


    //<editor-fold desc="Document">
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseDocument(FileResult.OnResultListener listener, FileType.Type... fileType) {
        mOnResultListener = listener;
        try {
            getDocument().launch(fileType);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseDocumentList(FileResult.OnResultListListener listener, FileType.Type... fileType) {
        mOnResultListListener = listener;
        try {
            getDocumentList().launch(fileType);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }
    //</editor-fold>


    //<editor-fold desc="ImageVideo">
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseImageVideo(FileResult.OnResultListener listener) {
        chooseDocument(listener, FileType.Type.IMAGE, FileType.Type.VIDEO);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseImageVideoDocumentList(FileResult.OnResultListListener listener) {
        chooseDocumentList(listener, FileType.Type.IMAGE, FileType.Type.VIDEO);
    }
    //</editor-fold>


    //<editor-fold desc="Image">
    public void chooseImage(FileResult.OnResultListener listener) {
        chooseContent(listener, FileType.Type.IMAGE);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void chooseImageList(FileResult.OnResultListListener listener) {
        chooseContentList(listener, FileType.Type.IMAGE);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseImageDocument(FileResult.OnResultListener listener) {
        chooseDocument(listener, FileType.Type.IMAGE);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseImageDocumentList(FileResult.OnResultListListener listener) {
        chooseDocumentList(listener, FileType.Type.IMAGE);
    }
    //</editor-fold>


    //<editor-fold desc="Video">
    public void chooseVideo(FileResult.OnResultListener listener) {
        chooseContent(listener, FileType.Type.VIDEO);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void chooseVideoList(FileResult.OnResultListListener listener) {
        chooseContentList(listener, FileType.Type.VIDEO);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseVideoDocument(FileResult.OnResultListener listener) {
        chooseDocument(listener, FileType.Type.VIDEO);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void chooseVideoDocumentList(FileResult.OnResultListListener listener) {
        chooseDocumentList(listener, FileType.Type.VIDEO);
    }
    //</editor-fold>


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        mOnResultListener = null;
        mOnResultListListener = null;
        contentLauncher = null;
        contentListLauncher = null;
        documentLauncher = null;
        documentListLauncher = null;
        super.onDestroy(owner);
    }

}
